package com.wedo.mall.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.activity.EActivity;
import com.e.library.adapter.Adapter;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.listener.EListener;
import com.wedo.mall.R;
import com.wedo.mall.activity.agent.AgentDetailActivity;
import com.wedo.mall.dialog.SimpleDialog;
import com.wedo.mall.entity.Agent;
import com.wedo.mall.util.WUrl;
import com.wedo.mall.widget.StarView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * Created by lxy on 2017/6/19.
 */

public class AgentAdapter extends Adapter<Agent> {

    Context context;
    public Boolean isCollectionPage = false;
    public OnCollectionShopsFragmentListener ocsflistenr;

    private int layout = R.layout.item_shop;

    public static AgentAdapter main(Context context, List<Agent> datas) {
        return new AgentAdapter(context,datas,R.layout.item_shop);
    }

    public static AgentAdapter search(Context context, List<Agent> datas){
        return new AgentAdapter(context,datas,R.layout.item_shop_search);
    }

    public AgentAdapter(Context context, List<Agent> datas, int layout) {
        super(context, datas);
        this.layout = layout;
        this.context = context;
    }

    @Override
    protected int layout() {
        return layout;
    }

    @Override
    protected void bind(EHolder holder, final Agent agent, int position) {
        ImageView iv_img = holder.find(R.id.iv_img);
        TextView tv_name= holder.find(R.id.tv_name);
        TextView tv_distance = holder.find(R.id.tv_distance);
        StarView sv_star = holder.find(R.id.sv_star);

        RelativeLayout rl_root = holder.find(R.id.rl_root);


        tv_name.setText(agent.agent_name);
        tv_distance.setText(String.format("%skm",agent.distance));
        sv_star.star(agent.star);
        String logo = WUrl.url(agent.logo);
        Glide.with(mContext).load(logo).error(R.drawable.place_holder_agent).placeholder(R.drawable.place_holder_agent).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_img);

        if(isCollectionPage){
            rl_root.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    SimpleDialog dialog = SimpleDialog.with(context);
                    dialog.setQuestion(mContext.getString(R.string.deleteCollection));
                    dialog.listener = new SimpleDialog.OnEnsureListener() {
                        @Override
                        public void onEnsure() {
                            delCollect(agent.id);
                        }
                    };
                    dialog.show();
                    return true;
                }
            });
        }
    }

    public void listener(final EActivity activity){
        onItemClickListener = new EListener<Agent>() {
            @Override
            public void onInvoked(View view, Agent data, int position) {
                AgentDetailActivity.toDetail(activity,data.id);
            }
        };
    }

    public void collectionShoplistener(final EActivity activity){
        onItemClickListener = new EListener<Agent>() {
            @Override
            public void onInvoked(View view, Agent data, int position) {
                AgentDetailActivity.toDetail(activity,data.agent_id);
            }
        };
    }

    //判断是否为收藏页面
    public void isCollection(boolean b){
        isCollectionPage = b;
    }

    /**
     * 删除收藏
     * @param
     */
    private void delCollect(String id){

        Map<String,String> params = new HashMap<>();
        params.put("ids",id);
        Observable<EResponse> observable = EClient.with().api().post(EApi.USER_DELCOLLECT,params);
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                ocsflistenr.onRefreshClick();
            }

            @Override
            public void failure(String msg) {
                Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * fragment回调
     */
    public interface OnCollectionShopsFragmentListener {
        public void onRefreshClick();
    }

    /**
     * 监听
     * @param Listener
     */
    public void setOnRefreshClickListener (OnCollectionShopsFragmentListener  Listener) {
        this.ocsflistenr = Listener;
    }
}
